/**
 * Copyright(c) by JNet Smartware Group.
 * All rights reserved. Not distribution or redistribution without
 * any permission of JNet Smartware Group.
 * 
 * @author thuc
 * 2010/03/15
 * Program: SWTRegisterCustomerEntry.java
 */
package vn.smartware.co.client.register;

import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import vn.smartware.co.commons.Const;
import vn.smartware.co.commons.SWTUtils;
import vn.smartware.co.dto.MstCustomerDTO;
import vn.smartware.co.forms.RegisterCustomerForm;
import vn.smartware.co.framework.bean.AddressDTO;
import vn.smartware.co.framework.common.BaseConstant;
import vn.smartware.co.framework.exception.ClientException;
import vn.smartware.co.framework.exception.FatalException;
import vn.smartware.co.framework.exception.ServerException;
import vn.smartware.co.framework.exception.SessionTimeOutException;
import vn.smartware.co.framework.form.CommonSearchForm;
import vn.smartware.co.framework.plugin.LangResources;
import vn.smartware.co.framework.swt.base.JNetComposite;
import vn.smartware.co.framework.utils.MsgBoxUtils;
import vn.smartware.co.java.net.commons.Utils;

/**
 * SWTRegisterCustomerEntry
 * @author thuctt
 */
public class SWTRegisterCustomerEntry extends JNetComposite {

	private static Log log = LogFactory.getLog(SWTRegisterCustomerEntry.class);
	
	private Text txtTel;

	private Combo cmbNationality;

	private Text txtEmailDomain;

	private CLabel clblMode;

	private Button btnNew;

	private Button btnSearch;

	private Text txtCusNo;

	private Group grpSearch;
	
	private Button btnSave;

	private Button btnClear;

	private Text txtName;

	private Button btnClose;

	private Button btnDelete;
	
	private Group grpInfo;
	
	private Group grpAddress;

	private Text txtCity;

	private Text txtDistrict;

	private Text txtWard;

	private Text txtStreet;

	private Text txtHouseNo;

	private Text txtBirthday;

	private Text txtIDNumber;

	private CLabel clblMessage;

	private Text txtEmailName;

	private CLabel clblCusName;

	private String mode;
	
	private MstCustomerDTO tmpCustomer = null;
	
	/**
	 * Constructor.
	 * @param parent 
	 * @param style 
	 * @param prgName 
	 */
	public SWTRegisterCustomerEntry(Composite parent, int style, String prgName) { 
		super(parent, style, prgName);
		try {
			if (!initData()){
				fatalErrorMsgBox();
				return;
			}
			clearAction();
		} 
		catch (Exception e) {
			log.error("Exception.", e);
			fatalErrorMsgBox();
		}
	}
	
	/**
	 * init data to display
	 * @throws FatalException
	 */
	private boolean initData() throws FatalException {
		String[] items = Utils.getCountryList(true);
		if (items == null || items.length == 0){
			MsgBoxUtils.errorMsgBox("errors.COMBO_DATA_NOT_SET",
					new Object[] { LangResources.getText("SWTRegisterCustomerEntry.lblNationality") });
			return false;
		}
		cmbNationality.setItems(items);
		
		return true;
	}

	/**
	 * init controls
	 */
	protected void initialize() {
		
		grpSearch = new Group(this, SWT.NONE);
		grpSearch.setBounds(15, 10, 784, 57);
		grpSearch.setBackground(SWTResourceManager.getColor(234, 234, 234));

		final CLabel lblCusNo = new CLabel(grpSearch, SWT.CENTER | SWT.BORDER);
		lblCusNo.setBounds(10, 20, 100, 22);
		lblCusNo.setFont(BaseConstant.FONT_LINK_LABEL);
		lblCusNo.setForeground(BaseConstant.COLOR_LINK_LABEL);
		lblCusNo.setText(LangResources.getText("SWTRegisterCustomerEntry.lblCusNo"));
		lblCusNo.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		lblCusNo.addMouseTrackListener(new MouseTrackAdapter() {
			Display display = Display.getCurrent();
			public void mouseEnter(MouseEvent e) {
				display.syncExec(new Runnable() {
					public void run() {
						lblCusNo.setForeground(BaseConstant.COLOR_LINK_LABEL_MOUSE_ENTER);
					}
				});

			}
			public void mouseExit(MouseEvent arg0) {
				display.syncExec(new Runnable() {
					public void run() {
						lblCusNo.setForeground(BaseConstant.COLOR_LINK_LABEL);
					}
				});
			}
		});
		lblCusNo.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent arg0) {
				callCustomerSearch(txtCusNo, clblCusName);
			}
		});

		txtCusNo = new Text(grpSearch, SWT.BORDER);
		txtCusNo.setTextLimit(9);
		txtCusNo.setFont(BaseConstant.FONT_BASE_TEXT);
		txtCusNo.setBackground(BaseConstant.COLOR_TEXT_REQUIRED);
		txtCusNo.setBounds(116, 20, 70, 22);
		txtCusNo.addFocusListener(new FocusAdapter() {
			public void focusGained(final FocusEvent arg0) {
				clblMessage.setText("");
			}
			public void focusLost(final FocusEvent arg0) {
				cusNoLostFocus(txtCusNo, clblCusName);
			}
		});
		
		clblCusName = new CLabel(grpSearch, SWT.LEFT | SWT.SHADOW_IN | SWT.BORDER);
		clblCusName.setBounds(192, 20,190, 22);
		clblCusName.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		clblCusName.setBackground(BaseConstant.COLOR_BGR_LABEL);

		btnSearch = new Button(grpSearch, SWT.NONE);
		btnSearch.setBounds(422, 19,100, 23);
		btnSearch.setText(LangResources.getText("btnSearch"));
		btnSearch.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnSearch.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnSearch.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				searchAction();
			}
		});
		
		this.getShell().setDefaultButton(btnSearch);

		btnNew = new Button(grpSearch, SWT.NONE);
		btnNew.setBounds(528, 19, 100, 23);
		btnNew.setText(LangResources.getText("btnNew"));
		btnNew.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnNew.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnNew.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				newAction();
			}
		});

		clblMode = new CLabel(grpSearch, SWT.CENTER | SWT.SHADOW_IN | SWT.BORDER);
		clblMode.setForeground(SWTResourceManager.getColor(0, 0, 255));
		clblMode.setFont(BaseConstant.FONT_BOLD_LABEL);
		clblMode.setBackground(BaseConstant.COLOR_BGR_LABEL);
		clblMode.setBounds(660, 20, 100, 22);

		final Label line1 = new Label(this, SWT.HORIZONTAL | SWT.SEPARATOR);
		line1.setBounds(0, 73, 812, 15);
		line1.setBackground(SWTResourceManager.getColor(234, 234, 234));
		line1.setText("Label");
		
		btnDelete = new Button(this, SWT.NONE);
		btnDelete.setBounds(379, 636,100, 23);
		btnDelete.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnDelete.setText(LangResources.getText("btnDelete"));
		btnDelete.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnDelete.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				deleteAction();
			}
		});
		
		grpInfo = new Group(this, SWT.NONE);
		grpInfo.setSize(782, 515);
		grpInfo.setLocation(15, 94);
		grpInfo.setBackground(BaseConstant.COLOR_BGR_BASE);

		final CLabel lblName = new CLabel(grpInfo, SWT.BORDER | SWT.CENTER);
		lblName.setBounds(30, 25, 80, 22);
		lblName.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		lblName.setText(LangResources.getText("SWTRegisterCustomerEntry.lblName"));

		txtName = new Text(grpInfo, SWT.BORDER);
		txtName.setBounds(116, 25, 340, 22);
		txtName.setTextLimit(50);
		txtName.setFont(BaseConstant.FONT_BASE_TEXT);
		txtName.setBackground(BaseConstant.COLOR_TEXT_REQUIRED);
		
		final CLabel lblIDNumber = new CLabel(grpInfo, SWT.CENTER | SWT.BORDER);
		lblIDNumber.setBounds(30, 53,80, 22);
		lblIDNumber.setFont(BaseConstant.FONT_BASE_LABEL);
		lblIDNumber.setText(LangResources.getText("SWTRegisterCustomerEntry.lblIDNumber"));
		
		txtIDNumber = new Text(grpInfo, SWT.BORDER);
		txtIDNumber.setBounds(116, 53,138, 22);
		txtIDNumber.setTextLimit(15);
		txtIDNumber.setFont(BaseConstant.FONT_BASE_TEXT);
		txtIDNumber.setBackground(BaseConstant.COLOR_TEXT_NORMAL);

		final CLabel lblTel = new CLabel(grpInfo, SWT.CENTER | SWT.BORDER);
		lblTel.setBounds(30, 81, 80, 22);
		lblTel.setFont(BaseConstant.FONT_BASE_LABEL);
		lblTel.setText(LangResources.getText("SWTRegisterCustomerEntry.lblTel")); 

		txtTel = new Text(grpInfo, SWT.BORDER);
		txtTel.setBounds(116, 81, 138, 22);
		txtTel.setTextLimit(15);
		txtTel.setFont(BaseConstant.FONT_BASE_TEXT);
		txtTel.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		
		final CLabel lblBirthday = new CLabel(grpInfo, SWT.CENTER | SWT.BORDER);
		lblBirthday.setBounds(30, 109,80, 22);
		lblBirthday.setFont(BaseConstant.FONT_BASE_LABEL);
		lblBirthday.setText(LangResources.getText("SWTRegisterCustomerEntry.lblBirthday"));
		
		txtBirthday = new Text(grpInfo, SWT.BORDER);
		txtBirthday.setBounds(116, 109,100, 22);
		txtBirthday.addFocusListener(new FocusAdapter() {
			public void focusLost(final FocusEvent arg0) {
				txtBirthday.setText(SWTUtils.OperatorUI.formatYYYYMMDD(txtBirthday.getText()));
			}
		});
		txtBirthday.setFont(BaseConstant.FONT_BASE_TEXT);
		txtBirthday.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		
		final Label lblYMDFormat = new Label(grpInfo, SWT.CENTER);
		lblYMDFormat.setBounds(222, 113,100, 18);
		lblYMDFormat.setFont(BaseConstant.FONT_BASE_LABEL);
		lblYMDFormat.setBackground(BaseConstant.COLOR_BGR_BASE);
		lblYMDFormat.setText("(YYYY/MM/DD)");

		final CLabel lblNationality = new CLabel(grpInfo, SWT.CENTER | SWT.BORDER);
		lblNationality.setBounds(30, 138, 80, 22);
		lblNationality.setFont(BaseConstant.FONT_BASE_LABEL);
		lblNationality.setText(LangResources.getText("SWTRegisterCustomerEntry.lblNationality"));

		cmbNationality = new Combo(grpInfo, SWT.READ_ONLY);
		cmbNationality.setBounds(116, 137, 138, 24);
		cmbNationality.setTextLimit(200);
		cmbNationality.setFont(BaseConstant.FONT_BASE_LABEL);
		cmbNationality.setBackground(BaseConstant.COLOR_TEXT_REQUIRED);
		
		final CLabel lblEmail = new CLabel(grpInfo, SWT.CENTER | SWT.BORDER);
		lblEmail.setBounds(30, 166, 80, 22);
		lblEmail.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		lblEmail.setText(LangResources.getText("SWTRegisterCustomerEntry.lblEmail"));
		
		txtEmailName = new Text(grpInfo, SWT.BORDER);
		txtEmailName.setBounds(116, 166, 138, 22);
		txtEmailName.setTextLimit(30);
		txtEmailName.setFont(BaseConstant.FONT_BASE_TEXT);
		txtEmailName.setBackground(BaseConstant.COLOR_TEXT_NORMAL);

		final Label lblAmoc = new Label(grpInfo, SWT.CENTER);
		lblAmoc.setBounds(260, 170, 14, 18);
		lblAmoc.setFont(BaseConstant.FONT_BASE_LABEL);
		lblAmoc.setBackground(BaseConstant.COLOR_BGR_BASE);
		lblAmoc.setText("@");

		txtEmailDomain = new Text(grpInfo, SWT.BORDER);
		txtEmailDomain.setBounds(280, 166, 176, 22);
		txtEmailDomain.setTextLimit(30);
		txtEmailDomain.setFont(BaseConstant.FONT_BASE_TEXT);
		txtEmailDomain.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		
		grpAddress = new Group(grpInfo, SWT.NONE); 
		grpAddress.setText(LangResources.getText("SWTRegisterCustomerEntry.grpAddress"));
		grpAddress.setFont(BaseConstant.FONT_BASE_LABEL);
		grpAddress.setSize(426, 168);
		grpAddress.setLocation(30, 205);
		grpAddress.setBackground(BaseConstant.COLOR_BGR_BASE);
		
		final Label lblHouseNo = new Label(grpAddress, SWT.CENTER);
		lblHouseNo.setBounds(66, 24,17, 18);
		lblHouseNo.setFont(BaseConstant.FONT_BASE_LABEL);
		lblHouseNo.setBackground(BaseConstant.COLOR_BGR_BASE);
		lblHouseNo.setText(LangResources.getText("SWTRegisterCustomerEntry.lblHouseNo"));
		
		txtHouseNo = new Text(grpAddress, SWT.BORDER);
		txtHouseNo.setBounds(89, 21,79, 22);
		txtHouseNo.setTextLimit(20);
		txtHouseNo.setFont(BaseConstant.FONT_BASE_TEXT);
		txtHouseNo.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		
		final Label lblStreet = new Label(grpAddress, SWT.CENTER);
		lblStreet.setBounds(45, 51,38, 18);
		lblStreet.setFont(BaseConstant.FONT_BASE_LABEL);
		lblStreet.setBackground(BaseConstant.COLOR_BGR_BASE);
		lblStreet.setText(LangResources.getText("SWTRegisterCustomerEntry.lblStreet"));
		
		txtStreet = new Text(grpAddress, SWT.BORDER);
		txtStreet.setBounds(89, 48,285, 22);
		txtStreet.setTextLimit(100);
		txtStreet.setFont(BaseConstant.FONT_BASE_TEXT);
		txtStreet.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		
		final Label lblWard = new Label(grpAddress, SWT.CENTER);
		lblWard.setBounds(35, 79,48, 18);
		lblWard.setFont(BaseConstant.FONT_BASE_LABEL);
		lblWard.setBackground(BaseConstant.COLOR_BGR_BASE);
		lblWard.setText(LangResources.getText("SWTRegisterCustomerEntry.lblWard"));
		
		txtWard = new Text(grpAddress, SWT.BORDER);
		txtWard.setBounds(89, 76,79, 22);
		txtWard.setTextLimit(15);
		txtWard.setFont(BaseConstant.FONT_BASE_TEXT);
		txtWard.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		
		final Label lblDistrict = new Label(grpAddress, SWT.CENTER);
		lblDistrict.setBounds(51, 107,32, 18);
		lblDistrict.setFont(BaseConstant.FONT_BASE_LABEL);
		lblDistrict.setBackground(BaseConstant.COLOR_BGR_BASE);
		lblDistrict.setText(LangResources.getText("SWTRegisterCustomerEntry.lblDistrict"));
		
		txtDistrict = new Text(grpAddress, SWT.BORDER);
		txtDistrict.setBounds(89, 104,136, 22);
		txtDistrict.setTextLimit(20);
		txtDistrict.setFont(BaseConstant.FONT_BASE_TEXT);
		txtDistrict.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		
		final Label lblCity = new Label(grpAddress, SWT.CENTER);
		lblCity.setBounds(19, 135,64, 18);
		lblCity.setFont(BaseConstant.FONT_BASE_LABEL);
		lblCity.setBackground(BaseConstant.COLOR_BGR_BASE);
		lblCity.setText(LangResources.getText("SWTRegisterCustomerEntry.lblCity"));
		
		txtCity = new Text(grpAddress, SWT.BORDER);
		txtCity.setBounds(89, 132,136, 22);
		txtCity.setTextLimit(20);
		txtCity.setFont(BaseConstant.FONT_BASE_TEXT);
		txtCity.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		
		final Label line2 = new Label(this, SWT.HORIZONTAL | SWT.SEPARATOR);
		line2.setBounds(0, 615, 812, 15);
		line2.setBackground(BaseConstant.COLOR_BGR_BASE);
		
		clblMessage = new CLabel(this, SWT.RIGHT | SWT.SHADOW_IN | SWT.BORDER);
		clblMessage.setBounds(15, 635, 250, 25);
		clblMessage.setForeground(SWTResourceManager.getColor(0, 0, 255));
		clblMessage.setFont(SWTResourceManager.getFont("Microsoft Sans Serif", 10, SWT.BOLD));
		clblMessage.setBackground(SWTResourceManager.getColor(232, 255, 251));
		clblMessage.setAlignment(SWT.CENTER);

		btnSave = new Button(this, SWT.NONE);
		btnSave.setBounds(485, 636, 100, 23);
		btnSave.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnSave.setText(LangResources.getText("btnSave"));
		btnSave.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnSave.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				clblMessage.setText("");
				saveAction();
			}
		});

		btnClear = new Button(this, SWT.NONE);
		btnClear.setBounds(591, 636, 100, 23);
		btnClear.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnClear.setText(LangResources.getText("btnClear"));
		btnClear.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnClear.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				clearAction();
			}
		});

		btnClose = new Button(this, SWT.NONE);
		btnClose.setBounds(697, 636, 100, 23);
		btnClose.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnClose.setText(LangResources.getText("btnClose"));
		btnClose.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnClose.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (!closeAction()){
					btnClose.setFocus();
				}
			}
		});
		
	}

	/**
	 * clearAction
	 */
	protected void clearAction() {
		
		SWTUtils.OperatorUI.clearAll(this.getChildren());
		Control[] buttons = new Control[]{
				btnDelete,
				btnSave
		};
		SWTUtils.OperatorUI.setEnableAll(buttons, false);
		SWTUtils.OperatorUI.setEnableAll(grpInfo.getChildren(), false);
		SWTUtils.OperatorUI.setEnableAll(grpSearch.getChildren(), true);
		txtCusNo.setFocus();
		
		this.mode = null;
	}

	/**
	 * deleteAction
	 */
	protected void deleteAction() {
		
		try {
			clblMessage.setText("");
			
			int ans = MsgBoxUtils.questionMsgBox("question.BE_SURE");
			if (ans == SWT.NO){
				btnDelete.setFocus();
				return;
			}
			
			this.mode = Const.MODE_DEL;
			clblMode.setText(mode);
			RegisterCustomerForm form = new RegisterCustomerForm();
			form.setActionForm("registerCustomerForm");
			form.setActionMethod("saveAction");
			form.setCustomer(tmpCustomer);
			form.setProcessMode(mode);
			
			form = (RegisterCustomerForm)execute(form);
			
			SWTUtils.OperatorUI.setEnableAll(grpInfo.getChildren(), false);
			SWTUtils.OperatorUI.setEnableAll(grpSearch.getChildren(), false);
			SWTUtils.OperatorUI.setEnableAll(new Control[]{btnDelete, btnSave}, false);
			
			btnClear.setFocus();
			MsgBoxUtils.setStatusLabelMessage(clblMessage, "info.DELETE_SUCCESSFUL");
			
		} catch (SessionTimeOutException e) {
			log.warn("SessionTimeOutException", e);
		}catch (ClientException e) {
			log.error("ClientException", e);
			fatalErrorMsgBox();
		} catch (FatalException e) {
			log.error("FatalException", e);
			fatalErrorMsgBox();
		}
	}

	/**
	 * displayAction
	 */
	protected void searchAction() {
		
		try {
			//
			if (Utils.isNullEmpty(txtCusNo.getText())){
				int ans = MsgBoxUtils.questionMsgBox("question.CREATE_NEW", 
						new Object[]{LangResources.getText("SWTCustomerSearch.lblSearchNo")});
				
				if (ans == SWT.NO){
					txtCusNo.setFocus();
					return;
				}
				newAction();
				return;
			}
			
			// check action
			if (!chkSearchInput()){
				return;
			}
			
			CommonSearchForm form = new CommonSearchForm();
			form.setActionForm("commonSearchForm");
			form.setActionMethod("getCustomerList");
			form.setNoStart(Utils.toEmpty(txtCusNo.getText()));
			form.setNoEnd(Utils.toEmpty(txtCusNo.getText()));
			
			form = (CommonSearchForm)execute(form);
			
			ArrayList list = form.getList();
			if (list == null || list.isEmpty()){
				int ans = MsgBoxUtils.questionMsgBox("question.CREATE_NEW_IF_NO_DATA_FOUND");
				if (ans == SWT.NO){
					txtCusNo.setFocus();
					return;
				}
				newAction();
				return;
			}
			
			tmpCustomer = (MstCustomerDTO)list.get(0);
			editAction();
			
		} catch (SessionTimeOutException e) {
			log.warn("SessionTimeOutException", e);
		} catch (ServerException e) {
			log.error("ServerException.", e);
			fatalErrorMsgBox();
		} catch (FatalException e) {
			log.error("FatalException.", e);
			fatalErrorMsgBox();
		}
	}
	
	/**
	 * check input of search info
	 * @return boolean
	 */
	private boolean chkSearchInput() throws ClientException {
		
		if (!SWTUtils.Check.checkRequired(txtCusNo, 
				LangResources.getText("SWTCustomerSearch.lblSearchNo"))) {
			btnSearch.setFocus();
			return false;
		}
		
		if (!SWTUtils.Check.checkNumeric(txtCusNo, 
				LangResources.getText("SWTCustomerSearch.lblSearchNo"))) {
			btnSearch.setFocus();
			return false;
		}
		
		return true;
	}
	
	/**
	 * check input of save info
	 * @return boolean
	 */
	private boolean chkSaveInput() throws ClientException {
		
		if (!SWTUtils.Check.checkRequired(txtName, 
				LangResources.getText("SWTRegisterCustomerEntry.lblName"))) {
			txtName.setFocus();
			return false;
		}
		
		if (!Utils.isNullEmpty(txtIDNumber.getText()) 
				&& !SWTUtils.Check.checkNumeric(txtIDNumber, 
						LangResources.getText("SWTRegisterCustomerEntry.lblIDNumber"))){
			return false;
		}
		
		if (!Utils.isNullEmpty(txtBirthday.getText())){
			//check format YYYY/MM/DD
			if (!SWTUtils.Check.checkYYYYMMDDFormat(txtBirthday.getText(), 
					LangResources.getText("SWTRegisterCustomerEntry.lblBirthday"))){
				txtBirthday.setFocus();
				return false;
			}
		}
		
		if (!SWTUtils.Check.checkRequired(cmbNationality, 
				LangResources.getText("SWTRegisterCustomerEntry.lblNationality"))) {
			cmbNationality.setFocus();
			return false;
		}
		
		if (!SWTUtils.Check.checkNumeric(txtTel, 
				LangResources.getText("SWTRegisterCustomerEntry.lblTel"))) {
			txtTel.setFocus();
			return false;
		}
		
		if (!Utils.isNullEmpty(txtEmailName.getText()) || !Utils.isNullEmpty(txtEmailDomain.getText())){
			if (!SWTUtils.Check.checkEmail(txtEmailName.getText() + "@" + txtEmailDomain.getText(),  
					LangResources.getText("SWTRegisterCustomerEntry.lblEmail"))) {
				txtEmailName.setFocus();
				return false;
			}
		}
		
		return true;
	}

	/**
	 * saveAction
	 */
	protected void saveAction() {
		
		try {
			if (!chkSaveInput()){
				return;
			}
			//get data to save
			getSaveInput();
			//
			RegisterCustomerForm form = new RegisterCustomerForm();
			form.setActionForm("registerCustomerForm");
			form.setActionMethod("saveAction");
			form.setCustomer(tmpCustomer);
			form.setProcessMode(mode);
			
			form = (RegisterCustomerForm)execute(form);
			
			if (mode.equals(Const.MODE_NEW)){
				txtCusNo.setText(form.getCustomerNo());
			}
			
			searchAction();
			
			MsgBoxUtils.setStatusLabelMessage(clblMessage, "info.SAVE_SUCCESSFUL");

		} catch (SessionTimeOutException e) {
			log.warn("SessionTimeOutException", e);
		} catch (ServerException e) {
			log.error("ServerException.", e);
			fatalErrorMsgBox();
		} catch (FatalException e) {
			log.error("FatalException.", e);
			fatalErrorMsgBox();
		}
	}
	
	/**
	 * getSaveInput
	 *
	 */
	private void getSaveInput() {

		tmpCustomer.setName(Utils.toEmpty(txtName.getText()));
		tmpCustomer.setIdNumber(Utils.toEmpty(txtIDNumber.getText()));
		tmpCustomer.setBirthday(Utils.toEmpty(txtBirthday.getText()));
		tmpCustomer.setTel(Utils.toEmpty(txtTel.getText()));
		if (!Utils.isNullEmpty(txtEmailName.getText()) 
				&& !Utils.isNullEmpty(txtEmailDomain.getText())){
			tmpCustomer.setEmail(Utils.toEmpty(txtEmailName.getText()) + 
					"@" + Utils.toEmpty(txtEmailDomain.getText()));
		}else{
			tmpCustomer.setEmail("");
		}
		AddressDTO addr = new AddressDTO();
		addr.setHouseNo(Utils.toEmpty(txtHouseNo.getText()));
		addr.setStreet(Utils.toEmpty(txtStreet.getText()));
		addr.setWard(Utils.toEmpty(txtWard.getText()));
		addr.setDistrict(Utils.toEmpty(txtDistrict.getText()));
		addr.setCity(Utils.toEmpty(txtCity.getText()));
		addr.setNationality(SWTUtils.ComboProcess.getSelectingValue(cmbNationality));
		tmpCustomer.setAddress(addr);
	}

	/**
	 * callCustomerSearch
	 * @param txtNo
	 * @param clblName
	 */
	private void callCustomerSearch(Text txtNo, CLabel clblName) {

		MstCustomerDTO cus = SWTUtils.SWTControlProcess.openCustomerSearchScreen(txtNo, clblName);
		if (null != cus){
			tmpCustomer = cus;
		}
	}
	
	/**
	 * editAction
	 */
	@Override
	protected void editAction() {
		
		AddressDTO addr = tmpCustomer.getAddress();
		txtCusNo.setText(tmpCustomer.getCustomerNo());
		clblCusName.setText(tmpCustomer.getName());
		txtName.setText(tmpCustomer.getName());
		txtIDNumber.setText(tmpCustomer.getIdNumber());
		txtTel.setText(tmpCustomer.getTel());
		txtBirthday.setText(tmpCustomer.getBirthday());
		SWTUtils.ComboProcess.setDisplayLabel(cmbNationality, addr.getNationality());
		String mail = tmpCustomer.getEmail();
		if (!Utils.isNullEmpty(mail)){
			txtEmailName.setText(mail.substring(0, mail.indexOf("@")));
			txtEmailDomain.setText(mail.substring(mail.indexOf("@") + 1));
		}
		//group address
		txtHouseNo.setText(addr.getHouseNo());
		txtStreet.setText(addr.getStreet());
		txtWard.setText(addr.getWard());
		txtDistrict.setText(addr.getDistrict());
		txtCity.setText(addr.getCity());
		//
		mode = Const.MODE_EDIT;
		clblMode.setText(mode);
		SWTUtils.OperatorUI.setEnableAll(grpSearch.getChildren(), false);
		SWTUtils.OperatorUI.setEnableAll(grpInfo.getChildren(), true);
		SWTUtils.OperatorUI.setEnableAll(new Control[]{btnDelete, btnSave}, true);
		txtName.setFocus();
	}

	/**
	 * newAction
	 */
	@Override
	protected void newAction() {
		
		tmpCustomer = new MstCustomerDTO();
		mode = Const.MODE_NEW;
		//
		SWTUtils.OperatorUI.setEnableAll(grpSearch.getChildren(), false);
		SWTUtils.OperatorUI.setEnableAll(grpInfo.getChildren(), true);
		SWTUtils.OperatorUI.clearAll(grpSearch.getChildren());
		SWTUtils.OperatorUI.clearAll(grpInfo.getChildren());
		SWTUtils.OperatorUI.setEnableAll(new Control[]{btnSave}, true);
		//
		clblMode.setText(mode);
		txtName.setFocus();
	}
	
	/**
	 * cusNoLostFocus
	 * @param txtCusNo
	 * @param clblCusName
	 */
	private void cusNoLostFocus(Text txtCusNo, CLabel clblCusName) {

		if (Utils.isNullEmpty(txtCusNo.getText())) {
			return;
		}
		MstCustomerDTO cus = SWTUtils.SWTControlProcess.textCustomerNoLostFocus(txtCusNo, clblCusName);
		if (null != cus){
			tmpCustomer = cus;
		}
	}

	/**
	 * lockScreen
	 * @param flg 
	 */
	protected void lockScreen(boolean flg) {
		
		SWTUtils.OperatorUI.setEnableAll(this.getChildren(), !flg);
	}

	@Override
	protected boolean loadData() throws FatalException {
		// TODO Auto-generated method stub
		return false;
	}
}